﻿using System;
using Microsoft.Xna.Framework;

namespace SAM3D
{
    public interface ITransformable
    {
        Transform Transform { get; }
    }

    public class Transform
    {
        public event EventHandler Changed = delegate {};
        private Vector3 _position = Vector3.Zero;
        private Quaternion _rotation = Quaternion.Identity;
        private Vector3 _scale = Vector3.One;

        public Vector3 Position
        {
            get { return _position; }
            set { _position = value; Changed.Invoke(this, EventArgs.Empty); }
        }

        public Quaternion Rotation
        {
            get { return _rotation; }
            set { _rotation = value; Changed.Invoke(this, EventArgs.Empty); }
        }

        public Vector3 Scale
        {
            get { return _scale; }
            set { _scale = value; Changed.Invoke(this, EventArgs.Empty); }
        }

        public Vector3 TransformVector(Vector3 a)
        {
            return Vector3.Multiply(Vector3.Transform(a, Rotation), Scale) + Position;
        }

        public static implicit operator Matrix(Transform t)
        {
            return Matrix.CreateScale(t.Scale)*Matrix.CreateFromQuaternion(t.Rotation)*
                   Matrix.CreateTranslation(t.Position);
        }
    }
}